package com.cwx.tools;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;

public class HttpUrlConnectionUtil {

	private static TrustManager myX509TrustManager = new X509TrustManager() {

		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType)
				throws CertificateException {
		}

		public void checkClientTrusted(X509Certificate[] chain, String authType)
				throws CertificateException {
		}
	};
	public static HttpsURLConnection getUrlsConnection(String urlStr)
			throws IOException, NoSuchAlgorithmException, KeyManagementException {
		//设置SSLContext
		SSLContext sslcontext = SSLContext.getInstance("TLS");
		sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null);
		//打开连接
		URL url = new URL(urlStr);
		HttpsURLConnection urlsConnection = (HttpsURLConnection)url.openConnection();
		//设置套接工厂
		urlsConnection.setSSLSocketFactory(sslcontext.getSocketFactory());
		urlsConnection
				.setRequestProperty(
						"User-Agent",
						"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36");
		urlsConnection
				.setRequestProperty("Accept",
						"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		urlsConnection.setRequestProperty("Connection", "keep-alive");
//		urlConnection.setRequestProperty("Upgrade-Insecure-Requests:", "1");
//		urlConnection.setRequestProperty("contentType",
//				"application/x-www-form-urlencoded; charset=utf-8");
		urlsConnection.setDoInput(true);
		urlsConnection.setDoOutput(true);
		return urlsConnection;
	}
	public static HttpURLConnection getUrlConnection(String urlStr)
			throws IOException {
		URL url = new URL(urlStr);
		HttpURLConnection urlConnection = (HttpURLConnection) url
				.openConnection();
		urlConnection
				.setRequestProperty(
						"User-Agent",
						"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36");
		urlConnection
				.setRequestProperty("Accept",
						"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		urlConnection.setRequestProperty("Connection", "keep-alive");
//		urlConnection.setRequestProperty("Upgrade-Insecure-Requests:", "1");
//		urlConnection.setRequestProperty("contentType",
//				"application/x-www-form-urlencoded; charset=utf-8");
		urlConnection.setDoInput(true);
		urlConnection.setDoOutput(true);
		return urlConnection;
	}

	public static void writeData(HttpURLConnection urlConnection, String data)
			throws IOException {
		OutputStream os = urlConnection.getOutputStream();
		os.write(data.getBytes());
		os.flush();

	}

	public static String readData(HttpURLConnection urlConnection,String charset)
			throws IOException {
		StringBuffer sb = new StringBuffer();
		InputStream is = urlConnection.getInputStream();
		InputStreamReader isr = new InputStreamReader(is,charset);
		BufferedReader br = new BufferedReader(isr);
		String line = "";
		while ((line = br.readLine()) != null){
			sb.append(line);
		}
		return sb.toString();
	}
	public static String getCookies(HttpURLConnection urlConnection){
		String cookie = "";
		Map<String, List<String>> map = urlConnection
				.getHeaderFields();
		if(map != null && !map.isEmpty() && map.containsKey("Set-Cookie")){
			List<String> temp = map.get("Set-Cookie");
			for (String s : temp) {
				cookie += s.charAt(s.length() -1 ) == ';'?s : s + ";";
			}
			return cookie;
		}
		return "";
	}
}
